package com.bwhx.eruler.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bwhx.eruler.bean.Buyer;
import com.bwhx.eruler.bean.Order;
import com.bwhx.eruler.bean.OrderBack;
import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Ruler;
import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.dao.BuyerDao;
import com.bwhx.eruler.dao.OrderBackDao;
import com.bwhx.eruler.dao.RulerDao;
import com.bwhx.eruler.dao.StaffDao;
import com.bwhx.eruler.service.OrderBackService;
import com.bwhx.eruler.util.StringUtils;

/**
 * @Description:
 */
@Service
public class OrderBackServiceImpl implements OrderBackService {
	@Autowired 
	OrderBackDao obDao;
	
	@Autowired
	RulerDao rulerDao;
	
	@Autowired
	BuyerDao buyerDao;
	
	@Autowired
	StaffDao staffDao;
	@Override
	public void insert(OrderBack ob) {
		if(1==ob.getoBType()){
			Buyer buyer = buyerDao.findById(ob.getoBBuyerId());
			ob.setoBBuyerName(buyer.getbName());
		}else if(0==ob.getoBType()){
			Staff buyer = staffDao.findById(ob.getoBBuyerId());
			ob.setoBBuyerName(buyer.getsName());

		}

		
		Staff kuguan = staffDao.findById(ob.getoBKuGuanId());
		Staff proposer = staffDao.findById(ob.getoBProposerId());
		ob.setoBProposerName(proposer.getsName());
		ob.setoBKuGuanName(kuguan.getsName());
		
		obDao.insert(ob);
		
	}
	@Override
	public List<OrderBack> list(Page p) {
		
		return obDao.list(p);
	}
	@Override
	public Integer count(OrderBack ob) {
		return obDao.count(ob);
	}
	@Override
	public OrderBack get(Serializable id) {
		return obDao.get(id);
	}
	@Override
	public void update(OrderBack ob, List<Ruler> rulers, List<Ruler> paramrulers) {
		obDao.update(ob);
		rulerDao.updateByOrderBack(paramrulers);
		
		if(1==ob.getoBType()){
			Integer number0=0;
			Integer number1=0;
			Integer number2=0;
			Integer number3=0;
			for (Ruler ruler : rulers) {
				switch (ruler.getrVersion()) {
					case 0:
						number0++;
						break;
					case 1:
						number1++;
						break;
					case 2:
						number2++;
						break;
					case 3:
						number3++;
						break;
				}
			}
			
			HashMap<String, Integer> paramMap = new HashMap<>();
			paramMap.put("id", ob.getoBBuyerId());
			paramMap.put("number", -rulers.size());
			paramMap.put("number0", -number0);
			paramMap.put("number1", -number1);
			paramMap.put("number2", -number2);
			paramMap.put("number3", -number3);
			buyerDao.update(paramMap);
			
			
		}
	}
	
	


	
	@Override
	public List<Order> find(Order order) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	@Override
	public void delete(Serializable id) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void delete(Serializable[] ids) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public List<Order> findByName(String order) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void inputOrder(Order d) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public List<Order> orderListAll() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void batchInsert(ArrayList<Order> orders) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void del(String oBId) {
		obDao.delete(oBId);

	}
	
	
}
